<template>
  <div class="mian-container">
    <div class="search">
      <el-input
        v-model="keyword"
        placeholder="请输入关键字查询"
        style="width: 200px"
        size="small"
      ></el-input>
      <el-button type="info" plain style="margin-left: 10px" size="small" @click="search"
        >查询</el-button
      >
      <el-button type="warning" plain style="margin-left: 10px" size="small" @click="reset"
        >重置</el-button
      >
    </div>

    <div class="table">
      <el-table :data="tableData" stripe>
        <el-table-column prop="items" label="商品图片">
          <template v-slot="scope">
            <el-image
              v-for="item in scope.row.items"
              :key="item._id"
              style="width: 80px; height: 60px; border-radius: 5px"
              :src="item.charProductId ? item.charProductId.productImage : item.productId.productImage"
              fit="fill"
            ></el-image>
          </template>
        </el-table-column>
        <el-table-column prop="items" label="商品名称" show-overflow-tooltip>
          <template v-slot="scope">
            <div v-for="item in scope.row.items" :key="item._id" style="height: 60px;text-align: left;">
              {{ item.charProductId ? item.charProductId.productName : item.productId.productName }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="userName" label="下单用户" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="orderNumber" label="订单编号" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="items" label="商品单价">
          <template v-slot="scope">
            <div v-for="item in scope.row.items" :key="item._id" style="height: 60px;text-align: left;">
              {{ item.price }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="items" label="商品数量">
          <template v-slot="scope">
            <div v-for="item in scope.row.items" :key="item._id" style="height: 60px;text-align: left;">
              {{ item.quantity}}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="totalPrice" label="订单总价" sortable>
        </el-table-column>
        <el-table-column prop="createdAt" label="下单时间" show-overflow-tooltip> </el-table-column>
        <el-table-column prop="status" label="订单状态">
        </el-table-column>
        <el-table-column label="操作" width="160" align="center">
          <template v-slot="scope">
            <el-button v-show="scope.row.status === '待发货'" plain type="primary" size="mini" @click="handleOrders(scope.row)">发货</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination">
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNo"
          :page-sizes="[5, 10, 20]"
          :page-size="pageSize"
          :total="total"
          layout="total,prev,pager,next"
        >
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import {reqOrdersInfo,searchOrder,handleOrder} from "@/api/order"
export default {
  name: "order",
  data() {
    return {
      keyword:"",
      tableData: [],
      pageNo: 1,
      pageSize: 10,
      total: 0,
    };
  },
  methods:{
    // 查询订单的回调
    async search(){
      let result = await searchOrder(this.keyword)
      if(result.code == 200){
        this.tableData = result.data
        this.total = result.total
        this.$message.success(result.msg)
      }else {
        this.$message.error(result.msg);
      }
    },
    // 点击重置按钮的回调
    reset(){
      this.keyword = ""
      this.pageNo = 1
      this.getOrdersInfo(this.pageNo,this.pageSize)
    },
    // 点击发货或退款按钮的回调
    async handleOrders(row){
      let data = {status:"待收货"}
      let result = await handleOrder(row._id,data)
      if(result.code == 200){
        this.$message.success(result.msg)
        this.pageNo = 1
        this.getOrdersInfo(this.pageNo,this.pageSize)
      }else{
        this.$message.error(result.msg)
      }
    },
    // 获取订单列表
    async getOrdersInfo(pageNo,pageSize){
      let result = await reqOrdersInfo(pageNo,pageSize)
      if(result.code == 200){
        this.tableData = result.data
        this.total = result.total
      }else {
        this.$message.error(result.msg);
      }
    },
    // 分页器
    handleCurrentChange(val) {
      this.pageNo = val;
      this.getcharProductList(this.pageNo, this.pageSize);
    },
    handleSizeChange(val) {
      this.pageNo = val;
      this.getcharProductList(this.pageNo, this.pageSize);
    },
  },
  mounted(){
    this.getOrdersInfo(this.pageNo,this.pageSize)
  },
  watch: {
    keyword:function (newVal, oldVal) {
      if (newVal === "") {
        this.pageNo = 1;
        this.getOrdersInfo(this.pageNo, this.pageSize);
      }
    },
  }
};
</script>

<style lang="scss" scoped>
.main-container {
  .search {
    padding: 10px;
    background-color: #fff;
    border-radius: 5px;
    box-shadow: 0 2px 5px 0 rgba(0, 0, 0, 0.1);
  }

  .table {
    margin-top: 5px;
    padding: 15px 10px;
    background-color: #fff;
    border-radius: 5px;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  }
}
</style>
